<?php

// 优惠券表

namespace app\console\model;

use think\Model;
use think\Request;
use app\console\model\GoodsType as GoodsTypeModel;
use app\console\model\MemberMessage as MemberMessageModel;
use think\Db;
class Coupon extends Model
{

    // 栏目名称
    protected function getTypeNameAttr($value, $data)
    {
        $TypeName = (new GoodsTypeModel)->where('id', $data['parenTid'])->value('title');

        return $TypeName;
    }
    // 模型事件
    protected static function init()
    {

        // 写入前
        self::event('before_write', function ($data) {

            //halt(implode(',',input()['single_id2']));
            audingcc($data);
            if (Request::instance()->param('interval_timer')) {

                $interval_timer = explode(' / ', Request::instance()->param('interval_timer'));

                $data->start_time = strtotime($interval_timer[0]);
                $data->end_time = strtotime($interval_timer[1]);
                //$data->shop_assignor_id = implode(',',(Request::instance()->param()['shop_assignor_id']));
                $system = Request::instance()->param('system');
                if($system == 2){ //后台发放优惠券
                    $data->shop_assignor_id = implode(',',(Request::instance()->param()['shop_assignor_id']));
                }
                $data->sort      = 0;
                $data->number = Request::instance()->param('number',1);
                $data->create_time = time();
                $data->type = input('type');
                if(input('type') == 1 || input('type') == 3){
                    $data->single_id = 0;
                }else{
                    if(input('type') == 2){
                        $data->single_id = input('single_id');
                    }
                    if(input('type') == 4){
                        $data->single_id = implode(',',input()['single_id2']);
                    }

                }


                $data->multi_pic = (serialize(Request::instance()->param('picArr/a')));// 组图

            }


        });
        self::event('after_insert', function ($data) {
           
            $id = Request::instance()->param('id');
            if($data->system == 2){ //后台发放优惠券
                foreach (Request::instance()->param()['shop_assignor_id'] as $k => $v){

                    $single = $data['single_id'];
                    if($data['type'] == 4){
                        $single = $data['single_id'] == 0 ? 0 : ','.$data['single_id'].',';
                    }
                    $d = [
                        'type' => $data->type,
                        'single_id' => $single,
                        'member_id'     => $v,
                        'coupon_id'     => $data->id,
                        'title'         => $data['title'],
                        'minus_price'  => $data['actual_price'],
                        'total_price' => $data['virtual_price'],
                        'content'       => $data['content'],
                        'start_time'    => $data['start_time'],
                        'end_time'      => $data['end_time'],
                        'status'        => '1',
                        'multi_pic' => (serialize(Request::instance()->param('picArr/a'))),
                        'create_time'   => time()
                    ];
                    for ($i=0;$i < $data->number;$i++){
                        Db::name('member_coupon')->insert($d);
                    }

                   (new \app\index\controller\MemberMessage())->jpushMessage($v,'恭喜您获得优惠券'.$data->number.'张',$data['title'],3);

                }
            }

//            $find = Db::name('member_coupon')->where('member_id',$data->member_id)->where('coupon_id',$data->id)->find();
////            if($find['end_time'] <= time() || $find['start_time'] > time()){
////                $data['end_time'] = time()+ 15552000;
////            }
//            $d = [
//                'member_id'     => $data->member_id,
//                'coupon_id'     => $data->id,
//                'title'         => $data['title'],
//                'minus_price'  => $data['actual_price'],
//                'total_price' => $data['virtual_price'],
//                'content'       => $data['content'],
//                'start_time'    => $data['start_time'],
//                'end_time'      => $data['end_time'],
//                'status'        => '1',
//                'create_time'   => time()
//            ];
//            if(empty($find)){
//                Db::name('member_coupon')->insert($d);
//            }else{
//                Db::name('member_coupon')->where('member_id',$data->member_id)->where('coupon_id',$data->id)->update($d);
//            }
//
//        });

        // 新增后
        // self::event('after_insert', function ($data) {
        //     $model = new MemberMessageModel();
        //     $model->save(['member_id' => '0', 'title' => $data->title, 'content' => $data->content, 'create_time' => time()]);
        // });

        });

        self::event('after_update', function ($data) {
            //已使用的优惠券不处理；
//            $member_coupon_id = Db::name('member_coupon')->where('coupon_id',$data->id)->where('status',2)->column('id');//先查询出来已使用会员优惠券的id集合
//            $member_coupon_member_id = Db::name('member_coupon')->where('coupon_id',$data->id)->where('status',2)->column('member_id');//
//            Db::name('member_coupon')->where('coupon_id',$data->id)->where('status',1)->delete();//删除所有优惠券未只用;
//            foreach (Request::instance()->param()['shop_assignor_id'] as $k => $v) {
//                if(!empty($member_coupon_member_id)){ //已使用不为空
//
//                    if(!in_array($v,$member_coupon_member_id)){ //当前会员不在 已使用里的 就是添加
//                        $d = [
//                            'member_id'     => $v,
//                            'coupon_id'     => $data->id,
//                            'title'         => $data['title'],
//                            'minus_price'  => $data['actual_price'],
//                            'total_price' => $data['virtual_price'],
//                            'content'       => $data['content'],
//                            'start_time'    => $data['start_time'],
//                            'end_time'      => $data['end_time'],
//                            'status'        => '1',
//                            'create_time'   => time()
//                        ];
//                     $ret =  Db::name('member_coupon')->insert($d);
//                    // halt($ret);
//                        (new \app\index\controller\MemberMessage())->jpushMessage($v,'恭喜您获得赠送优惠券一张',$data['title'],3);
//                    }
//                }else{
////                    halt(1);
//                    $d = [
//                        'member_id'     => $v,
//                        'coupon_id'     => $data->id,
//                        'title'         => $data['title'],
//                        'minus_price'  => $data['actual_price'],
//                        'total_price' => $data['virtual_price'],
//                        'content'       => $data['content'],
//                        'start_time'    => $data['start_time'],
//                        'end_time'      => $data['end_time'],
//                        'status'        => '1',
//                        'create_time'   => time()
//                    ];
//                    Db::name('member_coupon')->insert($d);
//                    (new \app\index\controller\MemberMessage())->jpushMessage($v,'恭喜您获得赠送优惠券一张',$data['title'],3);
//                }
//               // dump($ret);
//            }
           // halt($d);
//            Db::name('member_coupon')->where('coupon_id',$data->id)->where('status',1)->delete();//先删除未使用的优惠券;
//            foreach (Request::instance()->param()['shop_assignor_id'] as $k => $v){
//                $member_coupon = Db::name('member_coupon')->where('coupon_id',$data->id)->where('status',2)->select();//获取已使用的优惠券id 就是剩余优惠券的id
//                $member_coupon_member_id_list = Db::name('member_coupon')->where('coupon_id',$data->id)->where('status',2)->column('member_id');//获取已使用的优惠券id 就是剩余优惠券的id
//                $d = [
//                    'member_id'     => $v,
//                    'coupon_id'     => $data->id,
//                    'title'         => $data['title'],
//                    'minus_price'  => $data['actual_price'],
//                    'total_price' => $data['virtual_price'],
//                    'content'       => $data['content'],
//                    'start_time'    => $data['start_time'],
//                    'end_time'      => $data['end_time'],
//                    'status'        => '1',
//                    'create_time'   => time()
//                ];
//
//                foreach ($member_coupon as $k1 => $v1){
//                    if(in_array($v1['member_id'],Request::instance()->param()['shop_assignor_id'])){ //如果已使用还在当前重新选择的内 就是修改 已使用的 如果不在就是新来的数据那就是添加
//                        continue;
//                        //Db::name('member_coupon')->where('coupon_id')->where('id',$v1['id'])->update($d);//;
//                    }
//                }
//                $d = [
//                    'member_id'     => $v,
//                    'coupon_id'     => $data->id,
//                    'title'         => $data['title'],
//                    'minus_price'  => $data['actual_price'],
//                    'total_price' => $data['virtual_price'],
//                    'content'       => $data['content'],
//                    'start_time'    => $data['start_time'],
//                    'end_time'      => $data['end_time'],
//                    'status'        => '1',
//                    'create_time'   => time()
//                ];
//                Db::name('member_coupon')->insert($d);
//
//
//            }

        });

        self::event('after_delete', function ($data) {
            //删除未使用的
            Db::name('member_coupon')->where('coupon_id',$data->id)->where('status',1)->delete();//先查询出来已

        });
    }

    public function getAudingTextAttr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败',0=> 0];
        return $status[$data['auding_status']];
    }
    public function getAudingText1Attr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败',0=> 0];
        return $status[$data['auding_status1']];
    }
    public function getAudingText2Attr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败',0=> 0];
        return $status[$data['auding_status2']];
    }
    public function getAudingText3Attr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败',0=> 0];
        return $status[$data['auding_status3']];
    }
}